package com.example.logistics_manage.controller;

import com.example.logistics_manage.Utils.RandomNum;
import com.example.logistics_manage.pojo.User;
import com.example.logistics_manage.pojo.dto.UserDto;
import com.example.logistics_manage.pojo.dto.UserRoleDto;
import com.example.logistics_manage.result.restful.R;
import com.example.logistics_manage.result.restful.ResponseEunm;
import com.example.logistics_manage.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    IUserService userService;

    @Autowired
    RandomNum randomNum;
    /**
     * 登录验证生成token
     * @param user 用户账号密码
     * @return 用户信息
     */
    @PostMapping("/login")
    public R<UserDto> login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     * 验证码登录
     * @param code 验证码
     * @param phone 电话
     * @return 返回结果
     */
    @GetMapping("/loginByCode")
    R<UserDto> loginByCode(@RequestParam("code") Integer code,@RequestParam("phone")Long phone){
        return userService.loginByCode(code,phone);
    }

    /**
     * 获取验证码
     * @param phone 1
     * @return 1
     */
    @GetMapping("sendCode/{phone}")
    public R<String> loginOnPhone(@PathVariable("phone") String phone){
        User user = userService.findByPhone(Long.valueOf(phone));
        if (user == null){
            return new R<>(ResponseEunm.SMS_SEND_ERROR_PHONE_IS_NULL,null);
        }else {
            Integer code = randomNum.randomNum();
            userService.updateCodeByPhone(code,Long.valueOf(phone));
            userService.sendCheckCode(phone,String.valueOf(code));
            return new R<>(ResponseEunm.SUCCESS,null);
        }
    }

    /**
     * 用户注册
     * @param user 用户信息
     * @return 提示信息
     */
    @PostMapping("/register")
    public R<User> register(@RequestBody User user){
        return userService.register(user);
    }

    /**
     * 上传图片到阿里云对象存储
     * @param file 文件对象
     * @return 文件路径
     * @throws IOException  io
     */
    @PostMapping("/upload")
    public R<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        //获得上传文件的流
        InputStream inputStream = file.getInputStream();
        // 获得上传文件的名字
        String originalFilename = file.getOriginalFilename();
        String uploadUrl = userService.upload(inputStream, "rivtto", originalFilename);
        //返回r对象
        return new R<>(ResponseEunm.SUCCESS,uploadUrl);
    }

    /**
     * 根据手机号更改头像
     * @param phone 电话号码
     * @param image 头像路径
     * @return xx
     */
    @PutMapping("/updateImageByPhone")
    public R<User> updateImageByPhone(@Param("phone") Long phone,@Param("image")String image){
        return userService.updateImageByPhone(phone, image);
    }
    /**
     * 根据电话查找
     * @param phone 电话
     * @return 1
     */
    @GetMapping("/findByPhone/{phone}")
    public R<User> findByPhone(@PathVariable("phone") Long phone){
        User user = userService.findByPhone(phone);
        return new R<>(ResponseEunm.SUCCESS,user);
    }

    /**
     * 忘了干啥的未知代码，以后出bug来看
     * @param user 2
     * @return 2
     */
    @PostMapping("/updateUser")
    public R<User> updateUser(@RequestBody User user){
        if (userService.updateUser(user)==1){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.ADD_FAIL,null);
    }

    /**
     * 查找司机
     * @return 司机
     */
    @GetMapping("/selectByJobId")
    public R<List<User>> selectByJobId(){
        return new R<>(ResponseEunm.SUCCESS,userService.selectByJobId());
    }


    /**
     * 查找所有员工
     * @return 所有员工
     */
    @GetMapping("/selectAllEmp")
    public R<List<UserRoleDto>> selectAllEmp() {
        return new R<>(ResponseEunm.SUCCESS,userService.selectAllEmp()) ;
    }

    /**
     * 更新员工角色
     * @param user
     * @return
     */
    @PutMapping("/updateRole")
    public R<User> updateRole(@RequestBody User user){
        if (userService.updateRole(user)==1){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.UPDATE_FAIL,null);
    }

    /**
     * 条件查询员工
     * @param user 用户
     * @return xx
     */
    @PostMapping("/selectEmp")
    public R<List<User>> selectEmp(@RequestBody User user){
       return new R<>(ResponseEunm.SUCCESS,userService.selectEmp(user));
    }

    @GetMapping("/selectAdmin")
    public R<List<User>> selectAdmin(){
        return new R<>(ResponseEunm.SUCCESS,userService.selectAdmin());
    }

    @PostMapping ("/selectAdminBy")
    public R<List<User>> selectAdminBy(@RequestBody User user){
        return new R<>(ResponseEunm.SUCCESS,userService.selectAdminBy(user));
    }

    @DeleteMapping("/deleteAdmin/{id}")
    public R<User> deleteAdmin(@PathVariable("id") Integer id){
        userService.deleteAdmin(id);
        return new R<>(ResponseEunm.SUCCESS,null);
    }

    @GetMapping("/selectDriver")
    public R<List<User>> selectDriver(){
        return new R<>(ResponseEunm.SUCCESS,userService.selectDriver());
    }

    @PostMapping("/selectDriverBy")
    public R<List<User>> selectDriverBy(@RequestBody User user){
        return new R<>(ResponseEunm.SUCCESS,userService.selectDriverBy(user));
    }

    @GetMapping("/selectCustomer")
    public R<List<User>> selectCustomer(){
        return new R<>(ResponseEunm.SUCCESS,userService.selectCustomer());
    }

    @PostMapping("/selectCustomerBy")
    public R<List<User>> selectCustomerBy(@RequestBody User user) {
        return new R<>(ResponseEunm.SUCCESS, userService.selectCustomerBy(user));
    }
}
